package binarySearch;

/**
 * 符合下列属性的数组 arr 称为 山脉数组 ：
 * arr.length >= 3
 * 存在 i（0 < i < arr.length - 1）使得：
 * arr[0] < arr[1] < ... arr[i-1] < arr[i]
 * arr[i] > arr[i+1] > ... > arr[arr.length - 1]
 * 给你由整数组成的山脉数组 arr ，返回任何满足 arr[0] < arr[1] < ... arr[i - 1] < arr[i] > arr[i + 1] > ... > arr[arr.length - 1] 的下标 i 。
 * 输入：arr = [0,1,0]
 * 输出：1
 * @author gaolainan
 */
public class PeakIndexInMountainArray
{
    public static void main(String[] args)
    {
        int[] arr = {0,1,2,1,0};
        System.out.println(peakIndexInMountainArray(arr));
    }

    public static int peakIndexInMountainArray(int[] arr) {
        if (arr.length == 3){
            return 1;
        }
        int low = 0;
        int high = arr.length-1;
        while(low <= high){
            int mid = (high + low)/2;
            if (arr[mid] < arr[mid+1]){
                low = mid+1;
            }else{
                high = mid-1;
            }
        }

        return low;
    }
}
